﻿Option Strict On
Option Explicit On

Imports CObject
Imports ConnectionServer
Imports System.Data.SqlClient

Namespace CMT

    Public Class Room
        Inherits CObject.CObject

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal piRoomID As Integer)
            MyBase.New(piRoomID)
        End Sub

        Public Overrides Function Clone() As CObject.CObject
            Return New Room
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "RoomID"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "EXEC CMT_GetRoomsDetails {EventID}"
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "Properties"

        Public ReadOnly Property RoomID As Integer
            Get
                Return CastToInteger(GetProperty("RoomID"))
            End Get
        End Property

        Public Property RoomNameOrNumber As String
            Get
                Return CastToString(GetProperty("RoomNameNumber"))
            End Get
            Set(ByVal value As String)
                SetProperty("RoomNameNumber", value)
            End Set
        End Property

        Public Property Capacity As Integer
            Get
                Return CastToInteger(GetProperty("Capacity"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("Capacity", value)
            End Set
        End Property

        Public Property Projector As Boolean
            Get
                Return CastToBoolean(GetProperty("Projector"))
            End Get
            Set(ByVal value As Boolean)
                SetProperty("Projector", value)
            End Set
        End Property

        Public Property ConferancePhone As Boolean
            Get
                Return CastToBoolean(GetProperty("ConfPhone"))
            End Get
            Set(ByVal value As Boolean)
                SetProperty("ConfPhone", value)
            End Set
        End Property

        Public Property FlipChart As Boolean
            Get
                Return CastToBoolean(GetProperty("FlipChart"))
            End Get
            Set(ByVal value As Boolean)
                SetProperty("FlipChart", value)
            End Set
        End Property

        Public Property NetworkConnection As Boolean
            Get
                Return CastToBoolean(GetProperty("NetworkConnection"))
            End Get
            Set(ByVal value As Boolean)
                SetProperty("NetworkConnection", value)
            End Set
        End Property

        Public Property WhiteBoard As Boolean
            Get
                Return CastToBoolean(GetProperty("WhiteBoard"))
            End Get
            Set(ByVal value As Boolean)
                SetProperty("WhiteBoard", value)
            End Set
        End Property

        Public Property PhoneNumber As Double
            Get
                Return CastToDouble(GetProperty("PhoneNumber"))
            End Get
            Set(ByVal value As Double)
                SetProperty("PhoneNumber", value)
            End Set
        End Property

        Public Property EventID As Integer
            Get
                Return CastToInteger(GetProperty("EventID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("EventID", value)
            End Set
        End Property

        Public ReadOnly Property EventObj As CMTEvent
            Get
                Dim loObj As CMTEvent = New CMTEvent(EventID)
                If loObj.EventID <> 0 Then
                    Return loObj
                Else
                    'no data
                    Return Nothing
                End If
            End Get
        End Property

#End Region

#Region "Methods"

        ''' <summary>
        ''' Add an event room
        ''' </summary>
        ''' <param name="eventId"></param>
        ''' <param name="roomName"></param>
        ''' <param name="phoneNumber"></param>
        ''' <param name="roomCapacity"></param>
        ''' <param name="hasFlipchart"></param>
        ''' <param name="hasNetwork"></param>
        ''' <param name="hasWhiteboard"></param>
        ''' <param name="hasProjector"></param>
        ''' <param name="hasConferencePhone"></param>
        ''' <returns>rows affected count</returns>
        ''' <remarks></remarks>
        Public Function AddRoom(ByVal eventId As Integer, ByVal roomName As String, ByVal phoneNumber As String, ByVal roomCapacity As String, _
                                ByVal hasFlipchart As Boolean, ByVal hasNetwork As Boolean, ByVal hasWhiteboard As Boolean, _
                                ByVal hasProjector As Boolean, ByVal hasConferencePhone As Boolean) As Integer
            Dim message As String
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim recordsAffected As Integer = 0
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventId", SqlDbType.Int).Value = eventId
                loCommand.Parameters.Add("@pRoomName", SqlDbType.NVarChar, 100).Value = roomName
                If Not String.IsNullOrEmpty(phoneNumber) Then
                    loCommand.Parameters.Add("@pRoomPhoneNumber", SqlDbType.NVarChar, 100).Value = phoneNumber
                End If

                If Not String.IsNullOrEmpty(roomCapacity) Then
                    loCommand.Parameters.Add("@pRoomCapacity", SqlDbType.Int).Value = Convert.ToInt32(roomCapacity)
                End If

                loCommand.Parameters.Add("@pHasFlipchart", SqlDbType.Bit).Value = hasFlipchart
                loCommand.Parameters.Add("@pHasNetworkConnectors", SqlDbType.Bit).Value = hasNetwork
                loCommand.Parameters.Add("@pHasWhiteboard", SqlDbType.Bit).Value = hasWhiteboard
                loCommand.Parameters.Add("@pHasProjector", SqlDbType.Bit).Value = hasProjector
                loCommand.Parameters.Add("@pHasConferencePhone", SqlDbType.Bit).Value = hasConferencePhone

                ' Add Output Parameter
                Dim loParamInstance As SqlParameter = New SqlParameter("@Output", SqlDbType.NVarChar, 200)
                loParamInstance.Direction = ParameterDirection.Output
                loCommand.Parameters.Add(loParamInstance)

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_RoomAdd"

                'Execute the Query
                recordsAffected = loCommand.ExecuteNonQuery()
                loConn.Connection.Close()

                message = loCommand.Parameters("@Output").Value.ToString()
                Return recordsAffected
            End Using
        End Function

        ''' <summary>
        ''' Get all rooms linked to an event
        ''' </summary>
        ''' <param name="eventId"></param>
        ''' <returns>data table</returns>
        ''' <remarks></remarks>
        Public Function GetEventRooms(ByVal eventId As Integer) As DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQl As String = String.Empty
                Dim loDT As DataTable
                lsSQl = "CMT_GetEventRooms " & eventId
                loDT = loConn.GetDataTable(lsSQl)
                Return loDT
            End Using
        End Function

        ''' <summary>
        ''' Update an event room
        ''' </summary>
        ''' <param name="eventId"></param>
        ''' <param name="roomId"></param>
        ''' <param name="roomName"></param>
        ''' <param name="phoneNumber"></param>
        ''' <param name="roomCapacity"></param>
        ''' <param name="hasFlipchart"></param>
        ''' <param name="hasNetwork"></param>
        ''' <param name="hasWhiteboard"></param>
        ''' <param name="hasProjector"></param>
        ''' <param name="hasConferencePhone"></param>
        ''' <returns>rows affected</returns>
        ''' <remarks></remarks>
        Public Function UpdateRoom(ByVal eventId As Integer, ByVal roomId As Integer, ByVal roomName As String, ByVal phoneNumber As String, ByVal roomCapacity As String, _
                                ByVal hasFlipchart As Boolean, ByVal hasNetwork As Boolean, ByVal hasWhiteboard As Boolean, _
                                ByVal hasProjector As Boolean, ByVal hasConferencePhone As Boolean) As Integer
            Dim message As String
            Dim recordsAffected As Integer = 0

            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventId", SqlDbType.Int).Value = eventId
                loCommand.Parameters.Add("@pRoomId", SqlDbType.Int).Value = roomId
                loCommand.Parameters.Add("@pRoomName", SqlDbType.NVarChar, 100).Value = roomName
                If Not String.IsNullOrEmpty(phoneNumber) Then
                    loCommand.Parameters.Add("@pRoomPhoneNumber", SqlDbType.NVarChar, 100).Value = phoneNumber
                End If

                If Not String.IsNullOrEmpty(roomCapacity) Then
                    loCommand.Parameters.Add("@pRoomCapacity", SqlDbType.Int).Value = Convert.ToInt32(roomCapacity)
                End If

                loCommand.Parameters.Add("@pHasFlipchart", SqlDbType.Bit).Value = hasFlipchart
                loCommand.Parameters.Add("@pHasNetworkConnectors", SqlDbType.Bit).Value = hasNetwork
                loCommand.Parameters.Add("@pHasWhiteboard", SqlDbType.Bit).Value = hasWhiteboard
                loCommand.Parameters.Add("@pHasProjector", SqlDbType.Bit).Value = hasProjector
                loCommand.Parameters.Add("@pHasConferencePhone", SqlDbType.Bit).Value = hasConferencePhone

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_RoomUpdate"

                'Execute the Query
                recordsAffected = loCommand.ExecuteNonQuery()
                loConn.Connection.Close()

                Return recordsAffected
            End Using
        End Function

        ''' <summary>
        ''' Check if an event room assigned to any meeting
        ''' </summary>
        ''' <param name="roomId"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function IsRoomAssignedToMeeting(ByVal roomId As Integer) As Boolean

            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pRoomId", SqlDbType.Int).Value = roomId

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_IsRoomAssignedToMeeting"

                'Execute the Query
                Dim isAssigned As Boolean = Convert.ToBoolean(loCommand.ExecuteScalar())
                loConn.Connection.Close()

                Return isAssigned
            End Using
        End Function

        ''' <summary>
        ''' To delete an event room
        ''' </summary>
        ''' <param name="roomId"></param>
        ''' <returns>affected row count</returns>
        ''' <remarks></remarks>
        Public Function DeleteRoom(ByVal roomId As Integer) As Integer
            Dim recordsAffected As Integer = 0
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pRoomId", SqlDbType.Int).Value = roomId

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_RoomDelete"

                'Execute the Query
                recordsAffected = loCommand.ExecuteNonQuery()
                loConn.Connection.Close()

                Return recordsAffected
            End Using
        End Function


        Public Function GetRoom(ByVal roomId As Integer) As DataTable
            Dim loDT As New DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pRoomId", SqlDbType.Int).Value = roomId

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_RoomGet"

                Dim sqlDataAdapter As New SqlDataAdapter(loCommand)
                sqlDataAdapter.Fill(loDT)

                loConn.Connection.Close()


            End Using
            Return loDT
        End Function

#End Region

    End Class

End Namespace